package com.sims.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.SimsTeacher;
import com.sims.service.ITeacherService;
import com.sims.service.impl.TeacherServiceImp;

public class TeacherPageAction extends ActionSupport {
	private String teacherId;
	private SimsTeacher teacher = null;
	ITeacherService teacherService = new TeacherServiceImp();

	// -----------分页获取数据---------------
	private int pageNo = 1;
	private int pageSize = 4;
	private int pageCount;
	private long rowCount;

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public SimsTeacher getTeacher() {
		return teacher;
	}

	public void setTeacher(SimsTeacher teacher) {
		this.teacher = teacher;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	// 获取所有账号
	public String listTeacher() throws Exception {
		System.out.println("-----listTeacherPage");
		rowCount = teacherService.getRowCount();
		// 获取总页数
		if (rowCount % pageSize == 0) {
			pageCount = (int) (rowCount / pageSize);
		} else {
			pageCount = (int) (rowCount / pageSize) + 1;
		}
		if (pageNo < 1) {
			pageNo = 1;
		} else if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		// 分页获取记录
		List<SimsTeacher> list = teacherService.getAllSimsTeacher(pageNo,
				pageSize);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("TeacherList", list);
		return "list";
	}

	// 删除老师
	public String deleteTeacher() throws Exception {
		System.out.println("-----deleteTeacher id=" + teacherId);
		Long id = Long.parseLong(teacherId);
		boolean bFlag = teacherService.deletSimsTeacherById(id);
		if (bFlag) {
			this.addActionMessage("删除数据成功！");
		} else {
			this.addActionMessage("删除数据失败！");
		}
		return listTeacher();
	}

	// 修改老师
	public String editTeacher() throws Exception {
		System.out.println("-----editTeacher id=" + teacherId);
		Long id = Long.parseLong(teacherId);
		teacher = teacherService.getSimsTeacherById(id);
		if (teacher == null) {
			// 未获取到老师对象给提示
			this.addActionError("id=" + id + ",未获取到老师对象！");
		}
		return "edit";
	}

	// 编辑老师
	@InputConfig(resultName = "edit_input")
	public String updateTeacher() throws Exception {
		System.out.println("-----updateTeacher");
		boolean bFlag = teacherService.updateSimsTeacher(teacher);
		if (bFlag) {
			// 修改成功
			return "go_list";
		} else {
			this.addActionError("您填写的工号已存在！请重新填写！");
			return "edit";
		}
	}

	// 编辑教师Action校验方法
	public void validateUpdateTeacher() throws Exception {
		System.out.println("----validateUpdateTeacher");
		if (teacher == null) {
			// 未获取到教师对象给提示
			this.addActionError("未获取到教师对象！");
		} else {
			if (teacher.getTeacherName() == null
					|| teacher.getTeacherName().trim().length() <= 0) {
				this.addFieldError("teacherNameInfo", "请输入名字 ！");
			}
			if (teacher.getTeacherNumber() == null
					|| teacher.getTeacherNumber().trim().length() <= 0) {
				this.addFieldError("teacherNumberInfo", "请输入工号！");
			}
			if (teacher.getDepartment() == null
					|| teacher.getDepartment().trim().length() <= 0) {
				this.addFieldError("teacherDepartmentInfo", "请填写系部 ！");
			}
			if (teacher.getMajor() == null
					|| teacher.getMajor().trim().length() <= 0) {
				this.addFieldError("teacherMajorInfo", "请填写专业！");
			}
		}
	}

	@InputConfig(resultName = "create_input")
	public String createTeacher() throws Exception {
		System.out.println("----createTeacher---");
		ITeacherService teacherService = new TeacherServiceImp();
		Long id = teacherService.createTeacher(teacher);
		if (id > 0) {
			// 新建成功
			this.addActionError(teacher.getTeacherNumber() + ",新建成功！");
			return listTeacher();
		} else {
			// 新建失败
			this.addActionError(teacher.getTeacherNumber() + "您填写的教师工号已存在！请重新填写！");
			return "create";
		}
	}

	// 注册Action校验方法
	public void validateCreateTeacher() throws Exception {
		System.out.println("----validateCreateTeacher---");
		if (teacher == null) {
			// 未获取到用户对象给提示
			this.addActionError("未获取到用户对象！");
		} else {
			if (teacher.getTeacherName() == null
					|| teacher.getTeacherName().trim().length() <= 0) {
				this.addFieldError("teacherNameInfo", "请输入名字 ！");
			}
			if (teacher.getTeacherNumber() == null
					|| teacher.getTeacherNumber().trim().length() <= 0) {
				this.addFieldError("teacherNumberInfo", "请输入工号！");
			}
			if (teacher.getDepartment() == null
					|| teacher.getDepartment().trim().length() <= 0) {
				this.addFieldError("teacherDepartmentInfo", "请填写系部 ！");
			}
			if (teacher.getMajor() == null
					|| teacher.getMajor().trim().length() <= 0) {
				this.addFieldError("teacherMajorInfo", "请填写专业！");
			}
		}
	}
}
